<?php

namespace App\Models;

use App\MongoModel as Model;
/**
 * Class WxClient
 * @package App\Models
 * @property string id
 * @property string mobile      微信手机号
 * @property string password    微信密码
 * @property string nickName    微信号昵称
 * @property string storeAt     手机号存放位
 * @property string userName    微信分配给用户的@UserName
 * @property int    uin         微信分配给用户的唯一性ID
 * @property string uuid        此微信号最新使用的uuid
 * @property string qrcode      此微信号加好友的二维码图片, Base64格式
 * @property string headImg     此微信使用的头像地址
 * @property string project_id  保存分配给微信号的项目ID, 没有project_id或者project_id为空的微信号为未使用。
 * @property \stdClass $clientInfo  微信客户端侧的信息
 * {
 *     userName: '',
 *     nickName: '',
 *     remarkName: '',
 *     city: '',
 *     uin: '',
 *     pYQuanPin: '',
 *     remarkPYInitial: '',
 * }
 * @property array contacts     微信好友
 * [
 *   {
 *     userName:'',        # 好友在微信中的唯一标识
 *     nickName:'',        # 好友的微信昵称
 *     customer_id: '',    # 好友在系统中的ID
 *     addTime: '',        # 好友添加的时间
 *   },
 *
 * ]
 * @property array groups
 */
class WxClient extends Model
{

    protected $fillable = ['mobile', 'password', 'nickName', 'storeAt', 'qrcode', 'uin'];
    protected $casts = [
        'clientInfo' => 'object',
    ];
    protected $appends = ['headImg', 'id', 'friendCount'];

    public function getHeadImgAttribute() {
        return null;
    }
    public function getFriendCountAttribute() {
        return !empty($this->contacts)?
            count($this->contacts):0;
    }
    // 微信号和project是多对多关系
    public function project() {
        return $this->belongsToMany(Project::class);
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function scopeSearch($query, $inputs)
    {
        if(!empty($inputs)){
            return $query->where(function ($query) use ($inputs){
               if(!empty($inputs['mobile'])){
                   $query->where('mobile', $inputs['mobile']);
               }
                if(!empty($inputs['nickName'])) {
                    $query->where('nickName', 'like', "%{$inputs['nickName']}%");
                }
                
                if (!empty($inputs['project_id'])) {
                    // project_id填写的是项目title
                    $project_ids = Project::where('title', 'like', "%{$inputs['project_id']}%")->get()->pluck('id')->all();
                    $pros = Project::find($project_ids);
                    $wx_ids = [];
                    foreach ($pros as $v) {
                        $wx_ids = array_merge($wx_ids, $v->wx_id);
                    }
                    $query->whereIn('_id', $wx_ids);
                }
                if (!empty($inputs['contact']['nickName'])) {
                    $inputs['contacts'] = array_dot($inputs['contact']);
                    $inputs['contacts'] = implode(',', $inputs['contacts']);
                    $query->where('contacts.nickName', 'like', "%{$inputs['contacts']}%");
                }
            });
        }
        return $query;
    }

    /**
     * @param $account
     * @return bool
     */
    public function canAddContactByAccount($account)
    {
//        if (isset($this->attributes['contacts'])) {
//            $contacts = collect($this->contacts);
//            $exists = $contacts->whereStrict('account', $account)->count();
//            return $exists === 0;
//        }
        return true;
    }
}
